package com.example.seh.person.buy;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

import com.example.seh.R;
import com.example.seh.person.buy.bought.MyBoughtFragment;
import com.example.seh.person.buy.errand.MyErrandFragment;
import com.example.seh.util.FragmentAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class BoughtActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager2 vp2;
    private List<Fragment> fragments;
    private FragmentAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bought);
        getViews();
        fragments = getDatas();
        adapter = new FragmentAdapter(fragments, BoughtActivity.this);
        vp2.setAdapter(adapter);
        vp2.setUserInputEnabled(false);
        TabLayoutMediator mediator = new TabLayoutMediator(
                tabLayout,
                vp2,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        switch (position) {
                            case 0 :
                                tab.setText("我买到的");
                                break;
                            case 1 :
                                tab.setText("我的快递");
                                break;
                        }
                    }
                }
        );

        mediator.attach();
    }

    private List<Fragment> getDatas() {
        List<Fragment> fragments = new ArrayList<>();

        fragments.add(new MyBoughtFragment());
        fragments.add(new MyErrandFragment());
        return fragments;
    }

    private void getViews() {
        tabLayout = findViewById(R.id.bought_tablayout);
        vp2 = findViewById(R.id.bought_vp2);
    }
}